
<?php
/**
 * class模板
 *
 * PHP version 7.2
 *
 * @category  API
 * @package   YII
 * @author    saruri <saruri@163.com>
 * @copyright 2006-2019 saruri
 * @license   https://saruri.cn/licence.txt BSD Licence
 * @link      http://saruri.cn
 * @date      2021/03/11 14:51:59
 */
//namespace console\models;
//use Yii;

//读取国旗 放到postmeta里
require_once('arr.php');
class CountryMeta
{
    private $_config = [];
    public $sql = '';
    //初始化
    public function __construct()
    {
        //各种配置
        new Terms();
        $re =new Terms();
        $this->countrys=$re->getCountryNameArr()->getTaxonomyArr()->getResult()->run();
        $this->mysql=$re->mysqli;
        $this->metaId=7063;//5566;
        $this->countrys['欧盟委员会总司']['flag']='world-flag_257---Europe Union';
        $this->countrys['欧盟委员总司']['flag']='world-flag_257---Europe Union';
        $this->countrys['联合国系统']['flag']='world-flag_258---United Nation';
        $this->countrys['欧盟']['flag']='world-flag_257---Europe Union';
        $this->countrys['东盟']['flag']='world-flag_260---ASEAN';

        $this->countrys['东帝汶']['flag']='world-flag_227-TLS--Timor-Leste';
        $this->countrys['伊朗']['flag']='world-flag_106-IRN--Iran Islamic Republic of';
        $this->countrys['柬埔寨']['flag']='world-flag_038-KHM--Cambodia';
        $this->countrys['阿拉伯联合酋长国']['flag']='world-flag_239-ARE--United Arab Emirates';
        $this->countrys['经济金融领域']['flag']='world-flag_258---United Nation';
        $this->countrys['粮食、能源、卫生、海事、民航等领域']['flag']='world-flag_258---United Nation';
        $this->countrys['知识产权、安全、法律领域']['flag']='world-flag_258---United Nation';
        $this->countrys['科技合作领域']['flag']='world-flag_258---United Nation';
        $this->countrys['英国']['flag']='world-flag_069-GB-ENG--England';
        $this->countrys['南苏丹']['flag']='world-flag_213-SSD--South Sudan';
        $this->countrys['波黑']['flag']='world-flag_028-BIH--Bosnia and Herzegovina';
        //flag/circle/svg/
        //var_dump($this->countrys['英国']);
        //exit("ces");
    }
      
    /*
    *  @desc      获取新增的 post
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 14:58:49
    */
    public function getPost()
    {
      
        $this->flagName='波黑国旗';
        //do somehing
        //$where = " WHERE post_excerpt <> '' ";
        //$where = " WHERE `post_excerpt`='英国'";
        $where = " WHERE `post_excerpt`='欧盟委员总司'";
        $where = " WHERE `post_excerpt`='波黑'";
        
        $SQL_SELECT_CODE="SELECT * FROM  `wp_posts`".$where;
        //exit($SQL_SELECT_CODE);
        $CODE=$this->mysql->query($SQL_SELECT_CODE);
        $i=0;
        while ($arr1=$CODE->fetch_array()) {
            $this->postArr[$i]['ID']=$arr1['ID'];
            $this->postArr[$i]['flag']='/flag/circle/svg/'.$this->countrys[$arr1['post_excerpt']]['flag'].'.svg';
            $i++;
        }
        return $this;
    }


    /*
    *  @desc      gouzao 联系
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 17:36:27
    */
    public function build()
    {
        //do somehing
        $this->errMsg='';
        foreach ($this->postArr as $key => $value) {
            # code...
            
            //
          
            $arrInsert['post_id']=$value['ID'];
            
            $arrInsert['flag']=$value['flag'];
            //$arrInsert['country']=$value['post_excerpt'];
            $this->sql.=$this->buildInserSql($arrInsert);
        }
        return $this;
    }



    /*
    *  @desc      构造插入sql
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 17:26:36
    */
    public function buildInserSql($arr)
    {
        //do somehing
        $SQL_INSERT_CODE='';
        if ($arr['post_id']=='') {
            return '';
        }
        $post_id=$arr['post_id'];
        $flag=$arr['flag'];
        $str='';
        $str='a:5:{s:5:"width";i:512;s:6:"height";i:512;s:4:"file";s:16:"'.$flag.'.";s:5:"sizes";a:0:{}s:10:"image_meta";a:12:{s:8:"aperture";s:1:"0";s:6:"credit";s:0:"";s:6:"camera";s:0:"";s:7:"caption";s:0:"";s:17:"created_timestamp";s:1:"0";s:9:"copyright";s:0:"";s:12:"focal_length";s:1:"0";s:3:"iso";s:1:"0";s:13:"shutter_speed";s:1:"0";s:5:"title";s:0:"";s:11:"orientation";s:1:"0";s:8:"keywords";a:0:{}}}';
        //$SQL_INSERT_CODE.="DELETE FROM `wp_postmeta` WHERE post_id = $post_id AND meta_key='_wp_attachment_metadata'; "."\r\n";
        //$SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_wp_attachment_metadata','$str');"."\r\n";

        //这个才是现实国旗的
        $SQL_INSERT_CODE="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_wp_attached_file','$flag');"."\r\n";

        //$SQL_INSERT_CODE.="UPDATE `wp_postmeta` SET meta_value='$link' WHERE meta_id = $meta_id ;"."\r\n";

        //INSERT INTO `wp_term_relationships` (post_id,_wp_attached_file) VALUES( ,18); 1127 -> 18 //亚洲的代码  1127 ->211 //不丹的国家代码
        //INSERT INTO `wp_term_relationships` (object_id,term_taxonomy_id) VALUES(1128 ,18);

        //
        //$SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_sites_link','$link');"."\r\n";
        //$SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_sites_country','$country');"."\r\n";

        //暂时不插入
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_edit_lock','1615483336:1');"."\r\n";
        //$SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_edit_last',1);"."\r\n";
        //$SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_wp_old_date','-0001-11-30');"."\r\n";
        //$SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_sites_order','    0');"."\r\n";

        //准备插入
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_visible','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_goto','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_is_min_app','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_sites_sescribe','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_sites_language','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_thumbnail','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_wechat_qr','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_down_version','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_down_size','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_dec_password','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_down_preview','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_down_formal','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_sites_screenshot','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_down_url_list','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_app_platform','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_seo_metakey','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_seo_title','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'views','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_seo_desc','');"."\r\n";


        return $SQL_INSERT_CODE;
    }
      
    //保存sql文件
    public function save($fileName, $string)
    {
        $newfolder  = 'c:\excel2';
        $fileName=$fileName.'_'.time();
        $fw = fopen($newfolder.'\\'.$fileName.".sql", "wr");
        //var_dump($fw);
        fputs($fw, $string);
        fclose($fw);
        return $this;
    }

    public function run()
    {
        //主流程
        //var_dump($this->countrys);
        //var_dump($this->postArr);
        //exit($this->errMsg2);
        //exit($this->errMsg);
        $this->save("insertFlagMeta_".$this->flagName, $this->sql);
        exit("wancheng ");
        //exit($this->sql);
    }
}


$country = new countryMeta();
$country->getPost()->build()->run();
